package com.framework.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.framework.annotation.SystemLog;
import com.framework.controller.index.BaseController;
import com.framework.entity.AfdcitytreeFormMap;
import com.framework.entity.ButtomFormMap;
import com.framework.entity.Params;
import com.framework.mapper.AfdcitytreeMapper;
import com.framework.plugin.PageView;
import com.framework.util.Common;
import com.framework.util.TreeObject;
import com.framework.util.TreeUtil;


/**
 * 
 * @author framework 
 * @date  2015-11-19
 * @Email: g5874589652@126.com
 * @version 4.0v
 */
@Controller
@RequestMapping("/afdcitytree/")
public class AfdcitytreeController extends BaseController {
	@Inject
	private AfdcitytreeMapper afdcitytreeMapper;
	/**
	 * @param model
	 *            存放返回界面的model
	 * @return
	 */
	@ResponseBody
	@RequestMapping("treelists")
	public AfdcitytreeFormMap findByPage(Model model) {
		AfdcitytreeFormMap afdcitytreeFormMap = getFormMap(AfdcitytreeFormMap.class);
		String order = " order by parentId asc";
		afdcitytreeFormMap.put("$orderby", order);
		List<AfdcitytreeFormMap> mps = afdcitytreeFormMap.findByNames();
		List<TreeObject> list = new ArrayList<TreeObject>();
		for (AfdcitytreeFormMap map : mps) {
			TreeObject ts = new TreeObject();
			Common.flushObject(ts, map);
			list.add(ts);
		}
		TreeUtil treeUtil = new TreeUtil();
		List<TreeObject> ns = treeUtil.getChildTreeObjects(list, 0);
		afdcitytreeFormMap = new AfdcitytreeFormMap();
		afdcitytreeFormMap.put("treelists", ns);
		return afdcitytreeFormMap;
	}

	@ResponseBody
	@RequestMapping("reslists")
	public List<TreeObject> reslists(Model model) throws Exception {
		AfdcitytreeFormMap afdcitytreeFormMap = getFormMap(AfdcitytreeFormMap.class);
		
		afdcitytreeFormMap.put("where", "parentId=0");
		
		List<AfdcitytreeFormMap> mps = afdcitytreeFormMap.findByWhere();
		List<TreeObject> list = new ArrayList<TreeObject>();
		for (AfdcitytreeFormMap map : mps) {
			TreeObject ts = new TreeObject();
			Common.flushObject(ts, map);
			list.add(ts);
		}
		TreeUtil treeUtil = new TreeUtil();
		List<TreeObject> ns = treeUtil.getChildTreeObjects(list, 0, "　");
		return ns;
	}

	@ResponseBody
	@RequestMapping("findByPage")
	public PageView findByPage(String draw, String length, String start) throws Exception {
		AfdcitytreeFormMap afdCityFormMap = getFormMap(AfdcitytreeFormMap.class);
		afdCityFormMap.put("$orderby", getorder());
		afdCityFormMap.put("$search", getsearch());
		afdCityFormMap = toFormMap(afdCityFormMap, draw, length, start);
		afdCityFormMap.findByAll();
		pageView.setData(AfdcitytreeFormMap.mapper().findCityPage(afdCityFormMap));
		return pageView;
	}
	
	
	@ResponseBody
	@RequestMapping("getprovince")
	public List<AfdcitytreeFormMap> getprovince() throws Exception {
		List<AfdcitytreeFormMap> provincelist = AfdcitytreeFormMap.mapper().getprovince();
		return provincelist;
	}
	/**
	 * @param model
	 *            存放返回界面的model
	 * @return
	 */
	@RequestMapping("list")
	public String list(Model model) {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/system/afdcitytree/list";
	}

	/**
	 * 跳转到修改界面
	 * 
	 * @param model
	 * @param resourcesId
	 *            修改城市信息ID
	 * @return
	 */
	@RequestMapping("editUI")
	public String editUI(Model model) {
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			AfdcitytreeFormMap formMap=new AfdcitytreeFormMap();
			model.addAttribute("afdcitytree", formMap.findById(id));
		}
		return Common.BACKGROUND_PATH + "/system/afdcitytree/edit";
	}

	/**
	 * 跳转到新增界面
	 * 
	 * @return
	 */
	@RequestMapping("addUI")
	public String addUI(Model model) {
		return Common.BACKGROUND_PATH + "/system/afdcitytree/add";
	}

	/**
	 * 权限分配页面
	 * 
	 * @author framework Email：g5874589652@126.com date：2015-4-14
	 * @param model
	 * @return
	 */
	@RequestMapping("permissions")
	public String permissions(Model model) {
		AfdcitytreeFormMap afdcitytreeFormMap = getFormMap(AfdcitytreeFormMap.class);
		List<AfdcitytreeFormMap> mps = afdcitytreeFormMap.findByWhere();
		List<TreeObject> list = new ArrayList<TreeObject>();
		for (AfdcitytreeFormMap map : mps) {
			TreeObject ts = new TreeObject();
			Common.flushObject(ts, map);
			list.add(ts);
		}
		TreeUtil treeUtil = new TreeUtil();
		List<TreeObject> ns = treeUtil.getChildTreeObjects(list, 0);
		model.addAttribute("permissions", ns);
		return Common.BACKGROUND_PATH + "/system/afdcitytree/permissions";
	}

	/**
	 * 添加城市
	 * 
	 * @param afdcitytree
	 * @return Map
	 * @throws Exception
	 */
	@RequestMapping("addEntity")
	@ResponseBody
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="城市管理",methods="资源管理-新增资源")//凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity() throws Exception {
		AfdcitytreeFormMap afdcitytreeFormMap = getFormMap(AfdcitytreeFormMap.class);
		String parentid=(String)afdcitytreeFormMap.get("parentId");
		int myparent=Integer.parseInt(parentid);
		int level=2;
		if (myparent==0) {
			level=1;
		}
		afdcitytreeFormMap.put("level", level);
		afdcitytreeFormMap.save();
		return "success";
	}

	/**
	 * 更新城市
	 * 
	 * @param model
	 * @param Map
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("editEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="城市管理",methods="资源管理-修改资源")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity(Model model) throws Exception {
		AfdcitytreeFormMap afdcitytreeFormMap = getFormMap(AfdcitytreeFormMap.class);
		if("2".equals(afdcitytreeFormMap.get("type"))){
			afdcitytreeFormMap.put("description", Common.htmltoString(afdcitytreeFormMap.get("description")+""));
		}
		Object o = afdcitytreeFormMap.get("ishide");
		if(null==o){
			afdcitytreeFormMap.put("ishide", "0");
		}
		afdcitytreeFormMap.update();
		return "success";
	}

	/**
	 * 根据ID删除城市
	 * 
	 * @param model
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("deleteEntity")
	@SystemLog(module="城市管理",methods="资源管理-删除资源")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity(Model model) throws Exception {
		String[] ids = getParaValues("ids");
		for (String id : ids) {
			AfdcitytreeFormMap afdcitytreeFormMap = new AfdcitytreeFormMap();
			afdcitytreeFormMap.deleteById(id);
		};
		return "success";
	}


	@RequestMapping("sortUpdate")
	@ResponseBody
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	public String sortUpdate(Params params) throws Exception {
		List<String> ids = params.getId();
		List<String> es = params.getRowId();
		List<AfdcitytreeFormMap> maps = new ArrayList<AfdcitytreeFormMap>();
		AfdcitytreeFormMap map = null;
		for (int i = 0; i < ids.size(); i++) {
			 map = new AfdcitytreeFormMap();
			map.put("id", ids.get(i));
			map.put("level", es.get(i));
			maps.add(map);
		}
		AfdcitytreeFormMap.mapper().updateSortOrder(maps);
		return "success";
	}

	@ResponseBody
	@RequestMapping("findRes")
	public List<AfdcitytreeFormMap> findUserRes() {
		AfdcitytreeFormMap afdcitytreeFormMap = getFormMap(AfdcitytreeFormMap.class);
		List<AfdcitytreeFormMap> rs = AfdcitytreeFormMap.mapper().findRes(afdcitytreeFormMap);
		return rs;
	}
	/**
	 * 验证城市是否存在
	 * 
	 * @param name
	 * @return
	 */
	@RequestMapping("isExist")
	@ResponseBody
	public boolean isExist(String name,String resKey) {
		AfdcitytreeFormMap afdcitytreeFormMap = getFormMap(AfdcitytreeFormMap.class);
		List<AfdcitytreeFormMap> r = afdcitytreeFormMap.findByNames();
		if (r.size()==0) {
			return true;
		} else {
			return false;
		}
	}
	
	@ResponseBody
	@RequestMapping("findByButtom")
	public List<ButtomFormMap> findByButtom(){
		return afdcitytreeMapper.findByWhere(new ButtomFormMap());
	}
	
}